package com.ljh.oj.model.vo;

import cn.hutool.json.JSONUtil;
import com.ljh.oj.model.dto.questionsubmit.JudgeInfo;
import com.ljh.oj.model.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;

/**
 * 题目提交视图
 *
 * @author <a href="https://github.com/liyupi">程序员lin</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Data
public class QuestionSubmitVO implements Serializable {

    /**
     * id
     */
    private Long id;

    /**
     * 提交语言
     */
    private String language;

    /**
     * 提交代码
     */
    private String code;

    /**
     * 判题信息(json数组)
     */
    private JudgeInfo judgeInfo;

    /**
     * 判题状态(0 待判题；1 判题中；2 判题成功)
     */
    private Integer status;

    /**
     * 题目 id
     */
    private Long questionId;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建用户信息
     */
    private UserVO user;

    /**
     * 题目信息
     */
    private QuestionVO questionVO;

    /**
     * 封装类转对象
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        if (questionSubmitVO == null) {
            return null;
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        BeanUtils.copyProperties(questionSubmitVO, questionSubmit);
        JudgeInfo voJudgeInfo = questionSubmitVO.getJudgeInfo();
        questionSubmit.setJudgeInfo(JSONUtil.toJsonStr(voJudgeInfo));
        return questionSubmit;
    }

    /**
     * 对象转封装类
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        if (questionSubmit == null) {
            return null;
        }
        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVO);
        JudgeInfo judgeInfo = JSONUtil.toBean(questionSubmit.getJudgeInfo(), JudgeInfo.class);
        questionSubmitVO.setJudgeInfo(judgeInfo);
        return questionSubmitVO;
    }
}
/**
 * 这个封装类转对象的字段填充是如何思考的
 * 封装类字段与实体类字段类型不一致，需要手动填充
 * 什么叫这个字段类型不一致呢
 * 比如我们实体类的标签列表都是String类型，而封装类是List<String>类型
 * 这里就需要去做一个转化
 */